#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import "FWIconDotView.h"


static CGFloat const pi = 3.14159;


@implementation FWIconDotView

@synthesize icon, inset;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        inset = 0;
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame inset:(CGFloat)ins
{
    if (self = [super initWithFrame:frame]) {
        inset = ins;
    }
    
    return self;
}

- (void)dealloc {
    [icon release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Drawing

- (void)drawRect:(CGRect) rect {
    [super drawRect:rect withGloss:NO];

    if (icon) {
        UIImage* image = [UIImage imageWithContentsOfFile:icon];
        if (image != nil) {
            CGRect iconRect = CGRectInset(rect, inset, inset);

            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSaveGState(context);
            CGContextBeginPath(context);
            CGContextAddArc(context,
                            iconRect.origin.x + iconRect.size.width / 2,
                            iconRect.origin.y + iconRect.size.height / 2,
                            iconRect.size.height / 2, 0,
                            2 * pi, 0);
            CGContextClosePath (context);
            CGContextClip (context);
            [image drawInRect:iconRect];
            CGContextRestoreGState(context);
        }
        else
            NSLog(@"LI:FWTodo: Cannot find image %@", icon);
    }
    
    [super drawGloss:rect];
}

@end
